﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Collections;
using System.Xml.Linq;
using System.IO;

namespace StripeOne.Web.Core.Configuration
{
    public class StripeOneXml
    {
        public StripeOneXml(string section)
        {
            this.Section = section;
        }

        public StripeOneXml(string file, bool isDictionary = false)
        {
            this.File = file;
            this.IsDictionary = isDictionary;
        }

        public StripeOneXml(string file, string section, bool isDictionary = false)
        {
            this.File = file;
            this.Section = section;
            this.IsDictionary = isDictionary;
        }

        private string File { get; set; }
        private string Section { get; set; }
        private bool IsDictionary { get; set; }

        private XDocument LoadXML()
        {
            var pathSeparator = Path.DirectorySeparatorChar;

            var path = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath) + pathSeparator + "Config" + pathSeparator;
            if (this.IsDictionary)
                path += "Dictionary" + pathSeparator;
            path += this.File;

            return XDocument.Load(path);
        }

        private string GetValue(string key)
        {
            XDocument xml = LoadXML();

            var xmlElements = (from item in xml.Root.Descendants(this.Section)
                               select item.Element(key)).FirstOrDefault();
            
            if (xmlElements == null)
            {
                xmlElements = (from item in xml.Root.Descendants("default")
                               select item.Element(key)).FirstOrDefault();
            }

            return xmlElements != null ? xmlElements.Value : "[" + this.Section + "|default" + "/" + key + "]";
        }

        protected string GetValue(string section, string key)
        {
            XDocument xml = LoadXML();

            var xmlElements = (from item in xml.Root.Descendants(section)
                               select item.Element(key)).FirstOrDefault();

            return xmlElements != null ? xmlElements.Value : "[" + section + "/" + key + "]";
        }

        protected string GetString(string key)
        {
            return this.GetValue(key);
        }

        protected int GetInt(string key)
        {
            int result = 0;
            int.TryParse(this.GetValue(key), out result);
            return result;
        }

        protected Decimal GetDecimal(string key)
        {
            Decimal result = 0;
            Decimal.TryParse(this.GetValue(key), out result);
            return result;
        }

        protected Double GetDouble(string key)
        {
            Double result = 0;
            Double.TryParse(this.GetValue(key), out result);
            return result;
        }

        protected float GetFloat(string key)
        {
            float result = 0;
            float.TryParse(this.GetValue(key), out result);
            return result;
        }

        protected bool GetBool(string key)
        {
            bool result;
            bool.TryParse(this.GetValue(key), out result);
            return result;
        }

        protected DateTime GetDateTime(string key)
        {
            DateTime result = new DateTime();
            DateTime.TryParse(this.GetValue(key), out result);
            return result;
        }
    }
}